package com.richardjaap.data;

import java.util.ArrayList;

public class Lobby {
	
	private static Lobby instance;
	
	public ArrayList<RouletteTable> tables;
	private ArrayList<Account> accounts;
	public ArrayList<Player> players;
	private Account loggedOnAccount = null;
	
	
	private Lobby()
    {
        tables = new ArrayList<RouletteTable>();
        tables.add(new RouletteTable(1));
        tables.add(new RouletteTable(2));
        tables.add(new RouletteTable(3));
        tables.add(new RouletteTable(4));
        tables.add(new RouletteTable(5));
        tables.add(new RouletteTable(6));
        players = new ArrayList<Player>();
    }

    public static Lobby getInstance()
    {
      if (instance == null)
          instance = new Lobby();
      return instance;
    }
    
    public ArrayList<Player> getPlayers(){
    	return players;
    }
    
    public ArrayList<Account> getAccounts(){
    	return accounts;
    }

	public RouletteTable getTable(int tableId) {
		for(RouletteTable table : tables){
			if(table.getTableId() == tableId){
				return table;
			}
		}
		return null;
	}
	
	public void addTable(int id){
		tables.add(new RouletteTable(id));
	}
	
	public Account getAccount(int id){
		for(Account account : accounts){
			if(account.getId() == id){
				return account;
			}
		}
		return null;
	}
	
	public Player getPlayer(int playerId){
		if(getAccount(playerId) != null){
			Account account = getAccount(playerId);
			return account.getPlayer();
		} else
			return null;
	}
	
	public void topUp(int playerId){
		Account account = getAccount(playerId);
		double cash = account.getCashTotal();
		if(cash  == 0){
			account.setCashTotal(2000);
		} else
			System.out.println("Your balance is not 0. You can only top up if you're out of virtual cash.");
	}
	
	public boolean logIn(String username, String password) {
		if (loggedOnAccount == null) {
			for (Account account : accounts) {
				if (account.getUsername().equalsIgnoreCase(username)) {
					if (account.getPassword().equalsIgnoreCase(password)) {
						account.setLoggedOn(true);
						loggedOnAccount = account;
					} else
						account.setLoggedOn(false);
				}
			}
			return true;
		} else {
			System.out.println("There is already a account logged in.");
			return false;
		}
	}
	
	public boolean LogOut(int playerId){
		if(loggedOnAccount != null){
			loggedOnAccount.setLoggedOn(false);
			loggedOnAccount = null;
			return true;
		} else {
			System.out.println("There is no account logged in.");
			return false;
		}
	}
	
	public boolean register(String username, String password, String confirmPassword){
		if(password.equalsIgnoreCase(confirmPassword)){
			accounts.add(new Account(username, password));
			return true;
		} else {
			System.out.println("Passwords do not match!");
			return false;
		}
	}
	
	public String getTableInfo(int tableId) {
		RouletteTable table = getTable(tableId);
		String tableInfo = "Table ID: " + tableId + "\nAmount of Players: "
				+ table.getPlayers().size();
		String playerInfo = "";
		for (Player player : table.getPlayers()) {
			playerInfo += player.getAccount().getUsername() + "        " + player.getCashBuyIn()
					+ "\n";
		}
		String info = tableInfo + "\n\n" + playerInfo;
		return info;

	}
}
